package org.jeecg.modules.demo.zhengchai.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.zhengchai.project.entity.Project;
import org.jeecg.modules.demo.zhengchai.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController("zhengchaiProjectController")
@RequestMapping("/zhengchai/project")
public class ProjectController {

    @Autowired
    @Qualifier("zhengchaiProjectService")
    private ProjectService projectService;


    /**
     * 获取项目列表
     * @param page 分页对象
     * @param project 查询条件
     * @return 分页结果
     */
    @GetMapping("/list")
    public ResponseEntity<Page<Project>> list(Page<Project> page, Project project) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>(project);
        Page<Project> projectPage = projectService.page(page, queryWrapper);
        return ResponseEntity.ok(projectPage);
    }

    /**
     * 添加项目
     * @param project 项目对象
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResponseEntity<Boolean> add(@RequestBody Project project) {
        boolean result = projectService.save(project);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改项目
     * @param project 项目对象
     * @return 是否修改成功
     */
    @PutMapping("/edit")
    public ResponseEntity<Boolean> edit(@RequestBody Project project) {
        boolean result = projectService.updateById(project);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除项目
     * @param id 项目ID
     * @return 是否删除成功
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> delete(@RequestParam("id") Long id) {
        boolean result = projectService.removeById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据ID查询项目
     * @param id 项目ID
     * @return 项目对象
     */
    @GetMapping("/queryById")
    public ResponseEntity<Project> queryById(@RequestParam("id") Long id) {
        Project project = projectService.getById(id);
        return ResponseEntity.ok(project);
    }
}
